package com.taotao.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.common.pojo.EasyUiTreeNode;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.mapper.TbContentCategoryMapper;
import com.taotao.pojo.TbContentCategory;
import com.taotao.pojo.TbContentCategoryExample;
import com.taotao.pojo.TbContentCategoryExample.Criteria;
import com.taotao.service.ContentCatService;

@Service("contentCatService")
public class ContentCatServiceImpl implements ContentCatService {
	@Autowired
	private TbContentCategoryMapper contentCategoryMapper;

	@Override
	public List<EasyUiTreeNode> getContentCatList(Long parentId) {
		TbContentCategoryExample example = new TbContentCategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(parentId);
		List<TbContentCategory> list = contentCategoryMapper.selectByExample(example);
		List<EasyUiTreeNode> nodes = new ArrayList<>();
		for (TbContentCategory contentCategory : list) {
			EasyUiTreeNode node = new EasyUiTreeNode();
			node.setId(contentCategory.getId());
			node.setText(contentCategory.getName());
			node.setState(contentCategory.getIsParent() ? "closed" : "open");
			nodes.add(node);
		}

		return nodes;
	}

	@Override
	public TaotaoResult createContentCategory(Long parentId, String name) {
		TbContentCategory contentCategory = new TbContentCategory();
		contentCategory.setParentId(parentId);
		contentCategory.setName(name);
		contentCategory.setIsParent(false);
		contentCategory.setSortOrder(1);
		contentCategory.setCreated(new Date());
		contentCategory.setUpdated(new Date());
		contentCategory.setStatus(1);

		contentCategoryMapper.insert(contentCategory);
		// 判断父节点是不是简单节点,如果是,则需要更新为父节点
		TbContentCategory contentCategory2 = contentCategoryMapper.selectByPrimaryKey(parentId);
		if (!contentCategory2.getIsParent()) {
			contentCategory2.setIsParent(true);
			contentCategoryMapper.updateByPrimaryKey(contentCategory2);
		}

		return TaotaoResult.ok(contentCategory);
	}

	@Override
	public TaotaoResult deleteContentCategory(Long parentId, Long id) {
		contentCategoryMapper.deleteByPrimaryKey(id);
		TbContentCategoryExample example = new TbContentCategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(parentId);
		List<TbContentCategory> list = contentCategoryMapper.selectByExample(example);
		if (list.isEmpty()) {
			TbContentCategory contentCategory = contentCategoryMapper.selectByPrimaryKey(parentId);
			contentCategory.setIsParent(false);
			contentCategoryMapper.updateByPrimaryKey(contentCategory);
		}

		return TaotaoResult.ok();
	}

	@Override
	public TaotaoResult updateContentCategory(Long id, String name) {
		TbContentCategoryExample example = new TbContentCategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andIdEqualTo(id);
		List<TbContentCategory> list = contentCategoryMapper.selectByExample(example);
		if (list != null && list.size() > 0) {
			TbContentCategory contentCategory = list.get(0);
			contentCategory.setName(name);
			contentCategory.setUpdated(new Date());
			contentCategoryMapper.updateByPrimaryKey(contentCategory);
			return TaotaoResult.ok();
		}
		return TaotaoResult.ok("要修改的记录不存在");
	}

}
